package com.xs.ahoi.hcxs.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.BookDao;
import com.xs.ahoi.hcxs.model.Book;
import com.xs.ahoi.hcxs.ui.fragment.BookMarkFragment;
import com.xs.ahoi.hcxs.ui.fragment.CatalogFragment;
import com.xs.ahoi.hcxs.utils.Constants;

import java.util.ArrayList;
import java.util.List;

public class BookCatalogActivity extends BaseActivity {

    @BindView(R.id.layoutTab)
    public TabLayout layoutTab;
    @BindView(R.id.viewpager)
    public ViewPager viewpager;
    @BindView(R.id.tvBookName)
    public TextView tvBookName;

    // 书籍ID
    private int bookId;
    // 书籍查询工具
    private BookDao bookDao;
    private Bundle bundle;
    // tablayout的标签页显示名
    private String[] titles = new String[]{"目录", "书签"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_catalog);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        bookDao = BookDao.getInstance();

        // 获取bookId，传入fragment
        bookId = getIntent().getIntExtra(Constants.BOOK_ID, 0);
        bundle = new Bundle();
        bundle.putInt(Constants.BOOK_ID, bookId);

        // 设置书名
        Book book = bookDao.getById(bookId);
        tvBookName.setText(book.getName());

        // 加入fragment
        addFragment();
    }

    private void backToContent(int position) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.POSITION, position);
        setResult(Constants.RESULT_CODE_CATALOG_OR_SEARCH, new Intent().putExtras(bundle));
        finish();
    }

    @OnClick({R.id.arrowLeft})
    public void onClick(View v) {
        switch (v.getId()) {
            // 左上角返回箭头
            case R.id.arrowLeft:
                super.onBackPressed();
                break;
            default:
                break;
        }
    }

    // 加入fragment
    private void addFragment() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(getCatalogFragment(bundle));
        fragments.add(getBookMarkFragment(bundle));
        CatalogTabLayoutAdapter adapter = new CatalogTabLayoutAdapter(getSupportFragmentManager(), fragments, titles);
        viewpager.setAdapter(adapter);
        layoutTab.setupWithViewPager(viewpager);
    }

    private CatalogFragment getCatalogFragment(Bundle bundle) {
        CatalogFragment fragment = new CatalogFragment();
        fragment.setArguments(bundle);
        fragment.setListener(this::backToContent);
        return fragment;
    }

    private BookMarkFragment getBookMarkFragment(Bundle bundle) {
        BookMarkFragment fragment = new BookMarkFragment();
        fragment.setArguments(bundle);
        fragment.setListener(this::backToContent);
        return fragment;
    }

    // 自定义adapter
    private class CatalogTabLayoutAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragmentList;

        private String[] titles;

        public CatalogTabLayoutAdapter(FragmentManager mFragmentManager, List<Fragment> fragmentList, String[] titles) {
            super(mFragmentManager);
            mFragmentList = fragmentList;
            this.titles = titles;
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment;
            if (position < mFragmentList.size()) {
                fragment = mFragmentList.get(position);
            } else {
                fragment = mFragmentList.get(0);
            }
            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (titles != null && titles.length > 0) {
                return titles[position];
            }
            return null;
        }
    }
}
